package com.huarun.main;

import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huarun.bean.HuaRunInfo;
import com.huarun.dao.HuaRunDao;
import com.huarun.http.HuaRunHttpClientUtil;

public class HuaRunUpdateNameMain {

	public static void main(String[] args) {
		Connection c = null;
		// TODO Auto-generated method stub
		String jdbcStr = "jdbc:mysql://localhost:3306/huarundb?user=root&password=123456&useUnicode=true&characterEncoding=utf8";
		HuaRunDao huaRunDao = new HuaRunDao();
		HuaRunHttpClientUtil huaRunHttp = HuaRunHttpClientUtil.getInstance();
		HuaRunInfo huaRunInfo = new HuaRunInfo();
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			c = DriverManager.getConnection(jdbcStr);
			System.out.println("Opened database successfully");
			while(true){
				huaRunInfo =huaRunDao.getOneZhanghaoUname(c);
				if(huaRunInfo.getCode()==null ||"".equals(huaRunInfo.getCode())) break;
				//param={"loginName":"15249185014","password":"a123456"}
				String loginUrl = "https://app.crv.com.cn/app_api/v1/dc-app-api/mobile/api/user/login";
				JSONObject jObj2 = new JSONObject();
				jObj2.put("loginName", huaRunInfo.getCode());
				jObj2.put("password", huaRunInfo.getPassword());
				System.out.println(jObj2.toJSONString());
				String data2 = "param="+URLEncoder.encode(jObj2.toJSONString(),"utf-8");
				if(huaRunInfo.getUniqueId()==null || "".equals(huaRunInfo.getUniqueId())) {
					String uuid = UUID.randomUUID().toString();
					uuid = uuid.replace("-", "");
					String uniqueId = uuid.substring(0, 16);
					huaRunInfo.setUniqueId(uniqueId);
				}
				
//				RequestConfig config = IPFactory.getKdRequestConfig("192.168.0.1:8080");
				huaRunInfo = huaRunHttp.sendHttpPostProxy(loginUrl, huaRunInfo,data2, null);
				if(huaRunInfo.getContent().contains("频繁")) return;
				//{"data":{"inquire":1,"id":1103960,"loginTime":"2019-09-02 18:51:28","userSession":"3B5FC34325790EF8371C4E0117D2EEA7","loginName":"13372007847"},"stateCode":0}
				JSONObject content = JSONObject.parseObject(huaRunInfo.getContent());
				JSONObject jdata = content.getJSONObject("data");
				String userId = jdata.getString("id");
				String userSession = jdata.getString("userSession");
				
				huaRunInfo.setUserId(userId);
				huaRunInfo.setUserSession(userSession);
				
//				String userInfoUrl = "https://app.crv.com.cn/app_api/v1/dc-app-api/mobile/api/user/info";
//				huaRunInfo = huaRunHttp.sendHttpGetProxy(userInfoUrl, huaRunInfo, null);
//				//{"data":{"inquire":0,"collectCount":0,"clerkCode":"","accountBalance":0,"nickname":"15895887847","uname":"锟斤拷锟斤拷锟斤拷","unReceiveOrderCount":0,"unSendOrderCount":0,"unreadMessageCount":0,"id":1107261,"cardNo":"86400153877708","shopId":"A00C","unPayOrderCount":0,"mobile":"15895887847","loginName":"15895887847","ncmsMemberId":"7826514809038415953"},"stateCode":0}
//				JSONObject userInfoData = JSONObject.parseObject(huaRunInfo.getContent()).getJSONObject("data");
//				String ncmsMemberId = userInfoData.getString("ncmsMemberId");
//				String shopId = userInfoData.getString("shopId");
//				String uname = userInfoData.getString("uname");
//				
//				//https://app.crv.com.cn/app_api/v1/dc-app-api/mobile/api/ncms/memberinfo
//				String memberinfo = "https://app.crv.com.cn/app_api/v1/dc-app-api/mobile/api/ncms/memberinfo";
//				huaRunInfo = huaRunHttp.sendHttpGetProxy(memberinfo, huaRunInfo, null);
//				//{"data":{"coupon":0,"memberlevel":0,"points":16003},"stateCode":0}
//				JSONObject memberInfoData = JSONObject.parseObject(huaRunInfo.getContent()).getJSONObject("data");
//				String points = memberInfoData.getString("points");
//				
//				//https://app.crv.com.cn/app_api/v1/dc-app-api/mobile/api/point/queryPoints
//				String queryPointsUrl = "https://app.crv.com.cn/app_api/v1/dc-app-api/mobile/api/point/queryPoints";
//				JSONObject queryPointsObj = new JSONObject();
//				queryPointsObj.put("page", 1);
//				queryPointsObj.put("pageCount", 12);
//				queryPointsObj.put("pointsType", "");
//				queryPointsObj.put("storeCode", "");
//				System.out.println(jObj2.toJSONString());
//				String queryPointsData = "param="+URLEncoder.encode(queryPointsObj.toJSONString());
//				huaRunInfo = huaRunHttp.sendHttpPostProxy(queryPointsUrl, huaRunInfo,queryPointsData, null);
//				//{"data":{"total":100,"pageCount":12,"items":[{"directionopt":"+","maturity_date":"2022-01-01 12:00:00","member_id":"7809250308228456929","merchant_code":"1651200000001","merchant_name":"锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷薰锟剿�","order_no":"8S0089002219102900083161","orgi_order_no":"","orgi_outre_fer_no":"","orgi_sub_order_no":"","outre_fer_no":"1620641552","pay_amount":3.5,"points":"3","points_rule":"","points_type":"100000","send_time":"","status":"1","store_code":"8S0089","store_name":"锟侥诧拷锟斤拷锟斤拷锟斤拷锟斤拷","sub_order_no":"","terminal_id":"0022","transaction_time":"2019-10-29 06:47:16","transaction_type":"1001"}],"page":1},"stateCode":0}
//				JSONObject pointsData = JSONObject.parseObject(huaRunInfo.getContent()).getJSONObject("data");
//				JSONArray items = pointsData.getJSONArray("items");
//				
//				
//				//执锟叫碉拷录锟斤拷锟斤拷
//				Class.forName("com.mysql.jdbc.Driver");
//				c = DriverManager.getConnection(jdbcStr);
//				System.out.println("Opened database successfully");
//				Map<String, String> mapList = new HashMap<String, String>();
//				mapList.put("telphone", huaRunInfo.getCode());
//				mapList.put("userId", userId);
//				mapList.put("uniqueId", uniqueId);
//				mapList.put("userSession", userSession);
//				mapList.put("ncmsMemberId", ncmsMemberId);
//				mapList.put("shopId", shopId);
//				mapList.put("uname", uname);
//				mapList.put("points", points);
//				huaRunDao.updateZhanghao(mapList, c);
//				if(items!=null && items.size()>0){
//					huaRunDao.updatePoinstItems(items, c, huaRunInfo.getCode());
//				}
				String memberCardRegistUrl = "https://app.crv.com.cn/app_api/v1/dc-app-api/mobile/api/memberCard/regist";
				
				JSONObject memberCardRegistObj = new JSONObject();
				memberCardRegistObj.put("mobile", huaRunInfo.getCode());
				String guestName = huaRunDao.getRandomUname(c);
//				memberCardRegistObj.put("guestName", "王先生");
				memberCardRegistObj.put("guestName", guestName);
				memberCardRegistObj.put("idCard", "");
				memberCardRegistObj.put("city", "1016");
				memberCardRegistObj.put("guestSex", "3");
				memberCardRegistObj.put("shopId", "8S0317");
				System.out.println(memberCardRegistObj.toJSONString());
				String queryPointsData = "param="+URLEncoder.encode(memberCardRegistObj.toJSONString(),"utf-8");
				huaRunInfo = huaRunHttp.sendHttpPostProxy(memberCardRegistUrl, huaRunInfo,queryPointsData, null);
				if(huaRunInfo.getContent().contains("成功")) {
					huaRunInfo.setUname(guestName);
					huaRunDao.updateZhanghaoWeb(huaRunInfo,c);
				}
			}
			
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
			System.out.println("close database successfully");
			if (c != null)
				try {
					c.close();
				} catch (SQLException e) {
				}
		}
	}

}
